package nl.saxion.webtech.roulette.webservice;

import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import nl.saxion.webtech.roulette.model.Lobby;
import nl.saxion.webtech.roulette.model.Player;
import nl.saxion.webtech.roulette.model.Table;
import nl.saxion.webtech.roulette.model.info.PlayerSummary;
import nl.saxion.webtech.roulette.model.info.TableSummary;

@Path("/lobby")
public class LobbyService {

	@GET
	@Path("/tableInfo/{tableId}")
	@Produces(MediaType.APPLICATION_JSON)
	public TableSummary getTableInfo( @PathParam("tableId") int tableId ) {
		Table table = Lobby.getInstance().getTable(tableId);
		ArrayList<PlayerSummary> playersInfo = new ArrayList<PlayerSummary>();
		for (Player player : table.getPlayers()) {
			playersInfo.add( new PlayerSummary( player.getUsername(), player.getAmountOfCash(), player.getPlayerId() ) );	
		}
		return new TableSummary(tableId, playersInfo.size(), playersInfo);
	}
	
	@GET
	@Path("/login")
	@Produces(MediaType.APPLICATION_JSON)
	public PlayerSummary login( @QueryParam("username") String username, @QueryParam("password") String password ) {
		
		Player player = Lobby.getInstance().logIn( username, password ); 
		return new PlayerSummary( player.getUsername(), player.getAmountOfCash(), player.getPlayerId() );
	}
}